/**
 * Created by Administrator on 2016/3/8 0008.
 */

$(function() {

    //首次打开页面时获取默认数据
    getCustomer($("#companyCode").val());
    onenergyenergyAnalysis($("#customerinfoid").val(), $("#startDate").val(), $("#endDate").val());
    regionenergyAnalysis($("#customerinfoid").val(), $("#startDate").val(), $("#endDate").val());
    $("text:contains('Highcharts.com')").empty();

    //选择一个客户
    $("#companyName").dblclick(function() {
        $.baseCustomerApi({
            getBaseValue: function(customer) {
                $("#customerinfoid").val(customer.id);
                $("#companyCode").val(customer.companycode);
                $("#companyName").val(customer.customername);
            },
        });
    });

    //执行查询操作
    $("#searchEnergy").click(function() {
        regionenergyAnalysis($("#customerinfoid").val(), $("#startDate").val(), $("#endDate").val());
        onenergyenergyAnalysis($("#customerinfoid").val(),  $("#startDate").val(), $("#endDate").val());
        getCustomer($("#companyCode").val());
    });
});

function getCustomer(companyCode){
    $.ajax({
        contentType: "application/json",
        url: "getCustomer.shtml",
        data: {"companyCode": companyCode},
        async: false,
        dataType: "json",
        success: function (data) {
            $("#code").html(data.companycode);
            $("#address").html(data.city+data.district+data.street);
            $("#acreage").html(data.employeenum);
            $("#squares").html(data.square);
        }
    })
}
function regionenergyAnalysis(customerinfoid, startDate, endDate) {
    var param = {
        "customerinfoid": customerinfoid,
        "startDate": startDate,
        "endDate": endDate
    };
    $.ajax({
        contentType: "application/json",
        url: "findPowerEveryRegion.shtml",
        data: param,
        async: false,
        dataType: "json",
        success: function(data) {
            var dates = _.uniq(_.sortBy(_.pluck(data, 'statisticsdate')));
            var series = [];
            _.each(data, function(item) {
                if(item.power>0){
                    var array = [];
                    array[0] = item.areaname;
                    array[1] = item.power;
                    series[series.length] = array;
                }
            });
            pie('regioncontainer', '区域用电分析表', dates[0]+ '~' + dates[dates.length-1], '用电量', series,false);
            $("#position").html(_.pluck(data, 'position')[0]);
        }
    });
}

function onenergyenergyAnalysis(customerinfoid,startDate, endDate) {
    var param = {
        "customerinfoid": customerinfoid,
        "startDate": startDate,
        "endDate": endDate
    };
    $.ajax({
        contentType: "application/json",
        url: "findPowerEveryDay.shtml",
        data: param,
        async: false,
        dataType: "json",
        success: function(data) {
            //获取所有日期
            var dates = _.uniq(_.sortBy(_.pluck(data, 'statisticsdate')));
            linear('onenengycontainer', '用电分析表', dates[0] + '~' + dates[dates.length-1], dates, '电量 (度)', '度', [{
                name: _.pluck(data, 'customername')[0],
                data: _.pluck(data, 'power')
            }],false);
        }
    });
}
